﻿using UnityEngine;
using System.Collections.Generic;

namespace Dict.Blo
{
    public class DictFieldBlo
    {

        static public List<Dict.DataModel.TableField> All()
        {
            return Dict.DictDataManager.Instance.tableFieldDao.CacheList;
        }

        static public Dict.DataModel.TableField GetOne(string id)
        {
            return Dict.DictDataManager.Instance.tableFieldDao.GetById(id);
        }

        static public List<string> GetFish(string id)
        {
            List<string> ids = new List<string>();

            var field = GetOne(id);
            var fishes = field.FishPoints.Trim().Split(new char[1] { ';'},System.StringSplitOptions.RemoveEmptyEntries);
            ids.AddRange(fishes);
            return ids;
        }
        
        static public List<string> GetMine(string id)
        {
            List<string> ids = new List<string>();
            var field = GetOne(id);
            var mines = field.MinePoints.Trim().Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
            ids.AddRange(mines);

            return ids;
        }

        static public List<string> GetMonster(string id)
        {
            List<string> ids = new List<string>();
            var field = GetOne(id);
            var monsters = field.MonsterPoints.Trim().Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
            ids.AddRange(monsters);
            return ids;
        }

        static public List<Vector2> GetSafeZone(string id,out SafeZoneType type)
        {
            var field = GetOne(id);
            type =  SafeZoneType.Triangle;
            var first = field.SafeZone.Substring(0, 1);
            if(first.ToLower()=="s")
            {
                type = SafeZoneType.Triangle;
                List<Vector2> pos = new List<Vector2>(3);
                var temp = field.SafeZone.Substring(2);
                var ps = temp.Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p0 = ps[0];
                var p1 = ps[1];
                var p2 = ps[2];

                var p0s = p0.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p1s = p1.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p2s = p2.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

                var vec0 = new Vector2(float.Parse(p0s[0]), float.Parse(p0s[1]));
                var vec1 = new Vector2(float.Parse(p1s[0]), float.Parse(p1s[1]));
                var vec2 = new Vector2(float.Parse(p2s[0]), float.Parse(p2s[1]));

                pos.Add(vec0);
                pos.Add(vec1);
                pos.Add(vec2);
                return pos;
            }
            else if(first.ToLower()=="y")
            {
                type = SafeZoneType.Circle;
                List<Vector2> pos = new List<Vector2>(2);
                var temp = field.SafeZone.Substring(2);
                var ps = temp.Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p0 = ps[0];
                var p1 = ps[1];

                var p0s = p0.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var vec0 = new Vector2(float.Parse(p0s[0]), float.Parse(p0s[1]));
                pos.Add(vec0);
                var vec1 = new Vector2(float.Parse(p1), float.Parse(p1));
                pos.Add(vec1);
                return pos;
            }
            else if(first.ToLower()=="f")
            {
                type = SafeZoneType.Rectangle;
                List<Vector2> pos = new List<Vector2>(2);
                var temp = field.SafeZone.Substring(2);
                var ps = temp.Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p0 = ps[0];
                var p1 = ps[1];

                var p0s = p0.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                var p1s = p1.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);

                var vec0 = new Vector2(float.Parse(p0s[0]), float.Parse(p0s[1]));
                var vec1 = new Vector2(float.Parse(p1s[0]), float.Parse(p1s[1]));

                pos.Add(vec0);
                pos.Add(vec1);
                return pos;
            }
            else
            {
                return null;
            }
        }
    }
}
